package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	//"github.com/astaxie/beego/session"
	"webApp/models"
)

type NewsController struct {
	beego.Controller
}

// var globalSessionsTest *session.Manager
// func init() {
// 	globalSessionsTest, _ = session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":3600}`)
// 	go globalSessionsTest.GC()
// }

//@router  /news [get]
func (this *NewsController) News() {
	msg := "光明"
	this.Data["Website"] = msg
	this.TplName = "html/index.html"
}

//@router  /news/search [get]
func (this *NewsController) Show() {
	id := this.GetString("id")
	name := this.GetString("name")
	this.Data["json"] = map[string]interface{}{"code": 200, "message": id + name}
	this.ServeJSON()
}

//@router  /news/:id [get]
func (this *NewsController) Ahow() {
	id := this.GetString(":id")
	this.Data["json"] = map[string]interface{}{"code": 200, "message": id}
	this.ServeJSON()
}

//@router  /news/searchAll [post]
func (this *NewsController) Thow() {
	var news models.News
	json.Unmarshal(this.Ctx.Input.RequestBody, &news)
	models.CreateNews(news)
	this.Data["json"] = map[string]interface{}{"code": 200, "data": news}
	this.ServeJSON()

	// this.Data["json"] = map[string]interface{}{"code": 200, "data": models.ListNews()}
	// 设置session 验证登陆
	// sess, _ := globalSessionsTest.SessionStart(this.Ctx.ResponseWriter, this.Ctx.Request)
	// sess.Set("Session", "set session")
	// sessionValue := sess.Get("Session")
	// beego.Notice("session:")
	// beego.Notice(sessionValue.(string))

}
